import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_alibc/flutter_alibc.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _platformVersion1 = 'Unknown';

  @override
  void initState() {
    super.initState();
    FlutterAlibc.initSdk().then((info) {
      setState(() {
        _platformVersion = info.message;
      });
      FlutterAlibc.isLogin().then((islogin){
        if (islogin) {
          FlutterAlibc.getLoginSession().then((value) => setState(() {
            _platformVersion1 = "已登录: ${value.nick}";
          }));
        }else{
          setState(() {
            _platformVersion1 = "未登录";
          });
        }
      });

    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text(_platformVersion),
              Text(_platformVersion1),
              // GestureDetector(
              //   child: Text(_platformVersion),
              //   onTap: () {
              //     FlutterAlibc.isLogin().then((value) => setState(() {
              //           _platformVersion = "$value";
              //         }));
              //   },
              // ),
              RaisedButton(
                child: Text("login"),
                onPressed: () async{
                  FlutterAlibc.login().then((result) {
                    setState(() {
                      _platformVersion1 = result.nick;
                    });
                  });
                },
              ),
              RaisedButton(
                child: Text("loginout"),
                onPressed: () {
                  FlutterAlibc.loginout().then((value) => setState(() {
                    _platformVersion1 = "${value?"退出成功":"退出失败"}";
                  }));
                },
              ),
              RaisedButton(
                child: Text("打开app"),
                onPressed: () {
                  FlutterAlibc.openByBizCode(
                    pid: "mm_112883640_11584347_72287650277",
                    shopId: "65626181",
                  );
                },
              ),
              RaisedButton(
                child: Text("打开web"),
                onPressed: () {
                  FlutterAlibc.openByUrl(
                      url:
                          "https://uland.taobao.com/coupon/edetail?e=oSSExmvWXYYGQASttHIRqdYQwfcs8zoyxKXGKLqne1Hsx8cAhaH1SiZlT35kVCJr5R4kLBbVNWVsYgQTrXiDpq0TeAL%2BmcF17w9v818T2zNzQzL%2FHTq%2BPBemP0hpIIPvjDppvlX%2Bob8NlNJBuapvQ2MDg9t1zp0RRkY43XGTK8ko1aiZVhb9ykMuxoRQ3C%2BH5vl92ZYH25Cie%2FpBy9wBFg%3D%3D&traceId=0b15099215669559409745730e&union_lens=lensId:0b0b9f56_0c4c_16cd5da2c7f_3b31&xId=PwB9ZSWQxCtEwHxtbQc8iynshj5KEW16KP6OV6MAlpGpKCKmVGQMnjwQNhiGQpRY1gFyQHtqnYiv5wxGKTyCdf&tj1=1&tj2=1&relationId=518419440&activityId=23f4487e169647bd98b0d7fb2645947a",
                      pid: "mm_112883640_11584347_72287650277");
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
